<?php

namespace app\admin\model\scanwork;

use think\Model;

class Equipment extends Model
{
    protected $name = 'scanwork_equipment';
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function workCenter()
    {
        return $this->belongsTo('WorkCenter', 'work_center_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public static function generateEquipmentCode()
    {
        $prefix = 'EQ-';
        $lastCode = self::order('id', 'desc')->value('equipment_code');
        if ($lastCode) {
            $lastNum = intval(substr($lastCode, -4));
            $newNum = str_pad($lastNum + 1, 4, '0', STR_PAD_LEFT);
        } else {
            $newNum = '0001';
        }
        return $prefix . $newNum;
    }
}

